﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace CPPEI.PM.Web
{
    /// <summary>
    /// Summary description for PMWebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class PMWebService : System.Web.Services.WebService
    {
        [WebMethod]
        public ServiceModel.ServiceResult Login(string account, string password)
        {
            return new ServiceModel.ServiceResult();
        }

        [WebMethod]
        public ServiceModel.SingleResult<Models.User> CreateUser(string account, string name, string email, string phone, string password)
        {
            CPPEI.Service.SystemManagement.User user = new CPPEI.Service.SystemManagement.User();
            user.Id = Guid.NewGuid();
            user.Account = account;
            user.Name = name;
            user.Email = email;
            user.Phone = phone;
            user.Password = password;
            user.CreatedTime = DateTime.Now;
            new CPPEI.Service.SystemManagement.SystemManagementService().CreateUser(user);

            ServiceModel.SingleResult<Models.User> result = new ServiceModel.SingleResult<Models.User>();
            result.Data = new Models.User();
            result.Data.Id = user.Id;
            result.Data.Account = user.Account;
            result.Data.Name = user.Name;
            result.Data.Email = user.Email;
            result.Data.Phone = user.Phone;
            result.Data.CreatedTime = user.CreatedTime;

            return result;
        }

        [WebMethod]
        public ServiceModel.ListResult<Models.User> GetUserList()
        {
            List<CPPEI.Service.SystemManagement.User> userList = new CPPEI.Service.SystemManagement.SystemManagementService().GetUserList();

            ServiceModel.ListResult<Models.User> result = new ServiceModel.ListResult<Models.User>();
            for (int index = 0; index < userList.Count; index++)
            {
                Models.User user = new Models.User();
                user.Id = userList[index].Id;
                user.Account = userList[index].Account;
                user.Name = userList[index].Name;
                user.Email = userList[index].Email;
                user.Phone = userList[index].Phone;
                user.CreatedTime = userList[index].CreatedTime;
                result.Datas.Add(user);
            }

            return result;
        }

        [WebMethod]
        public List<Models.Navigation> GetNavigationList(string account, Guid projectId)
        {
            CPPEI.Service.SystemManagement.Navigation projectNavigation = new CPPEI.Service.SystemManagement.SystemManagementService().GetNavigation(account, CPPEI.PM.Service.Model.BD_NavigationTargetType.Project, projectId.ToString());

            List<CPPEI.Service.SystemManagement.Navigation> navigationList = new CPPEI.Service.SystemManagement.SystemManagementService().GetNavigationList(account, projectNavigation.Id);
            List<Models.Navigation> result = new List<Models.Navigation>();
            for (int index = 0; index < navigationList.Count; index++)
            {
                Models.Navigation navigation = new Models.Navigation();
                navigation.Id = navigationList[index].Id;
                navigation.Caption = navigationList[index].Caption;
                navigation.WorkSheetId = new Guid(navigationList[index].TargetId);

                result.Add(navigation);
            }

            return result;
        }

        [WebMethod]
        public bool CreateWorkItemCategory(string account, Guid projectId, Models.WorkItemCategory workItemCategory)
        {
            CPPEI.PM.Service.Model.WorkItemCategory newWorkItemCategory = new Service.Model.WorkItemCategory();
            newWorkItemCategory.Id = workItemCategory.Id;
            newWorkItemCategory.Code = workItemCategory.Code;
            newWorkItemCategory.ProjectId = projectId;
            newWorkItemCategory.Caption = workItemCategory.Caption;
            newWorkItemCategory.CreatedTime = DateTime.Now;
            newWorkItemCategory.Creator = account;
            new CPPEI.PM.Service.Model.PMService().CreateWorkItemCategory(newWorkItemCategory);
            return true;
        }

        [WebMethod]
        public bool CreateWorkItem(string account, Guid projectId, Guid workItemCategoryId, Models.WorkItem workItem)
        {
            CPPEI.PM.Service.Model.WorkItem newWorkItem = new Service.Model.WorkItem();
            newWorkItem.Id = workItem.Id;
            newWorkItem.Code = workItem.Code;
            newWorkItem.WorkItemCategoryId = workItemCategoryId;
            newWorkItem.ProjectId = projectId;
            newWorkItem.Caption = workItem.Caption;
            newWorkItem.CreatedTime = DateTime.Now;
            newWorkItem.Creator = account;
            new CPPEI.PM.Service.Model.PMService().CreateWorkItem(newWorkItem);
            return true;
        }

        [WebMethod]
        public bool CreateWorkItem4WorkSheet(string account, Guid projectId, Guid workSheetId,
            Guid workItemCategoryId, Models.WorkItem workItem)
        {
            CPPEI.PM.Service.Model.WorkItem newWorkItem = new Service.Model.WorkItem();
            newWorkItem.Id = workItem.Id;
            newWorkItem.Code = workItem.Code;
            newWorkItem.WorkItemCategoryId = workItemCategoryId;
            newWorkItem.ProjectId = projectId;
            newWorkItem.Caption = workItem.Caption;
            newWorkItem.CreatedTime = DateTime.Now;
            newWorkItem.Creator = account;
            new CPPEI.PM.Service.Model.PMService().CreateWorkItemAndAttachWorkSheet(newWorkItem);
            return true;
        }

        [WebMethod]
        public bool DeleteWorkItem(string account, Guid projectId, Guid workItemCategoryId, Guid workItemId)
        {
            return true;
        }

        [WebMethod]
        public bool UpdateWorkItem(string account, Guid projectId, Guid workItemCategoryId, Models.WorkItem workItem)
        {
            return true;
        }

        [WebMethod]
        public bool RefWorkItem(string account,
            Guid projectId,
            Guid workSheetId,
            Guid workItemCategoryId,
            Guid parentWorkItemId,
            Guid workItemId,
            bool updateOtherSheet)
        {
            if (updateOtherSheet)
            {
                new CPPEI.PM.Service.Model.PMService().RelateWorkItemToRelatedWorkSheets(workItemCategoryId, parentWorkItemId, workItemId);
            }
            else
            {
                new CPPEI.PM.Service.Model.PMService().RelateWorkItem(workSheetId, workItemCategoryId, parentWorkItemId, workItemId);
            }
            return true;
        }

        [WebMethod]
        public ServiceModel.ListResult<Models.Project> GetProjectList(string account)
        {
            ServiceModel.ListResult<PM.Service.Model.Project> serviceResult = new CPPEI.PM.Service.Model.PMService().GetProjectList(account);

            ServiceModel.ListResult<Models.Project> listResult = new ServiceModel.ListResult<Models.Project>();
            for (int index = 0; index < serviceResult.Datas.Count; index++)
            {
                Models.Project project = new Models.Project();
                project.Id = serviceResult.Datas[index].Id;
                project.Caption = serviceResult.Datas[index].Caption;
                listResult.Datas.Add(project);
            }
            return listResult;
        }

        [WebMethod]
        public ServiceModel.ServiceResult CreateProject(string account, string caption)
        {
            return new CPPEI.PM.Service.Model.PMService().CreateProject(account, caption);
        }

        [WebMethod]
        public ServiceModel.ServiceResult CreateWorkSheet(string account, Guid projectId, string caption, Guid workItemCategoryId)
        {
            new CPPEI.PM.Service.Model.PMService().CreateWorkSheet(account, projectId, caption, workItemCategoryId);
            return new ServiceModel.ServiceResult();
        }

        [WebMethod]
        public Models.WorkSheet GetWorkSheet(Guid projectId, Guid workSheetId)
        {
            CPPEI.ServiceModel.SingleResult<CPPEI.PM.Service.Model.WorkSheet> result = new CPPEI.PM.Service.Model.PMService().GetWorkSheet(projectId, workSheetId);

            Models.WorkSheet workSheet = new Models.WorkSheet();
            workSheet.Id = result.Data.Id;
            workSheet.Caption = result.Data.Caption;

            return workSheet;
        }

        [WebMethod]
        public Models.WorkItemCategoryRef GetWorkItemCategory4WorkSheet(Guid workSheetId)
        {
            CPPEI.PM.Service.Domain.WorkItemCategoryRef category = new CPPEI.PM.Service.Model.PMService().GetWorkItemCategory4WorkSheet(workSheetId);


            Models.WorkItemCategoryRef result = new Models.WorkItemCategoryRef();
            result.Id = category.Id;
            result.Code = category.Code;
            result.Caption = category.Caption;
            result.Level = category.Level;

            return result;
        }

        /// <summary>
        /// 获取工作表的工作项类别
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="workSheetId"></param>
        /// <returns></returns>
        [WebMethod]
        public Models.WorkItemCategoryRef GetNextWorkItemCategory4WorkSheet(Guid workSheetId, Guid parentWorkItemCategoryId)
        {
            List<CPPEI.PM.Service.Domain.WorkItemCategoryRef> categoryList = new CPPEI.PM.Service.Model.PMService().GetWorkItemCategory4WorkSheet(workSheetId, parentWorkItemCategoryId);

            Models.WorkItemCategoryRef result = null;
            for (int index = 0; index < categoryList.Count; index++)
            {
                result = new Models.WorkItemCategoryRef();
                result.Id = categoryList[index].Id;
                result.Caption = categoryList[index].Caption;
                result.Code = categoryList[index].Code;
                result.Level = categoryList[index].Level;
                break;
            }
            return result;
        }

        [WebMethod]
        public List<Models.WorkItemRef> GetWorkItemList4WorkSheet(Guid workSheetId, Guid workItemCategoryId, Guid parentWorkItemId)
        {
            List<PM.Service.Domain.WorkItemRef> itemList = new CPPEI.PM.Service.Model.PMService().GetWorkItem4WorkSheet(workSheetId, workItemCategoryId, parentWorkItemId);

            List<Models.WorkItemRef> result = new List<Models.WorkItemRef>();
            for (int index = 0; index < itemList.Count; index++)
            {
                Models.WorkItemRef workItemRef = new Models.WorkItemRef();
                workItemRef.Id = itemList[index].Id;
                workItemRef.Caption = itemList[index].Caption;
                workItemRef.Code = itemList[index].Code;
                workItemRef.Level = itemList[index].Level;

                result.Add(workItemRef);
            }
            return result;
        }

        [WebMethod]
        public List<Models.WorkStageRef> GetWorkStageList4WorkSheet(Guid workSheetId, Guid parentWorkStageId)
        {
            List<PM.Service.Domain.WorkStageRef> itemList = new CPPEI.PM.Service.Model.PMService().GetWorkStageList4WorkSheet(workSheetId, parentWorkStageId);

            List<Models.WorkStageRef> result = new List<Models.WorkStageRef>();
            for (int index = 0; index < itemList.Count; index++)
            {
                Models.WorkStageRef workStageRef = new Models.WorkStageRef();
                workStageRef.Id = itemList[index].Id;
                workStageRef.Caption = itemList[index].Caption;
                workStageRef.Code = itemList[index].Code;
                workStageRef.ParentId = itemList[index].ParentId;
                workStageRef.Level = itemList[index].Level;

                result.Add(workStageRef);
            }
            return result;
        }

        [WebMethod]
        public List<Models.WorkStageItemRef> GetWorkStageItemList4WorkSheet(Guid workSheetId, Guid workStageId, Guid parentWorkItemId)
        {
            List<PM.Service.Domain.WorkStageItemRef> itemList = new CPPEI.PM.Service.Model.PMService().GetWorkStageItem4WorkSheet(workSheetId, workStageId, parentWorkItemId);

            List<Models.WorkStageItemRef> result = new List<Models.WorkStageItemRef>();
            for (int index = 0; index < itemList.Count; index++)
            {
                Models.WorkStageItemRef itemRef = new Models.WorkStageItemRef();
                itemRef.Id = itemList[index].Id;
                itemRef.Caption = itemList[index].Caption;
                itemRef.Code = itemList[index].Code;
                itemRef.ParentId = itemList[index].ParentId;
                itemRef.Level = itemList[index].Level;

                result.Add(itemRef);
            }
            return result;
        }

        [WebMethod]
        public Models.WorkItemCategory GetWorkItemCategory(Guid projectId, Guid workItemCategoryId)
        {
            Models.WorkItemCategory result = new Models.WorkItemCategory();

            PM.Service.Model.WorkItemCategory workItemCatgory = new CPPEI.PM.Service.Model.PMService().GetWorkItemCategory(projectId, workItemCategoryId);

            result.Id = workItemCatgory.Id;
            result.Caption = workItemCatgory.Caption;
            

            return result;
        }

        [WebMethod]
        public Models.WorkItemCategory GetWorkItemCategoryByCode(Guid projectId, string workItemCategoryCode)
        {
            Models.WorkItemCategory result = new Models.WorkItemCategory();

            PM.Service.Model.WorkItemCategory workItemCategory = new CPPEI.PM.Service.Model.PMService().GetWorkItemCategoryByCode(projectId, workItemCategoryCode);

            result.Id = workItemCategory.Id;
            result.Caption = workItemCategory.Caption;
            result.Code = workItemCategory.Code;

            return result;
        }

        [WebMethod]
        public Models.WorkItem GetWorkItemByCode(Guid projectId, Guid workItemCategoryId, string workItemCode)
        {
            Models.WorkItem result = new Models.WorkItem();

            PM.Service.Model.WorkItem workItem = new CPPEI.PM.Service.Model.PMService().GetWorkItemByCode(projectId, workItemCategoryId, workItemCode);

            result.Id = workItem.Id;
            result.Caption = workItem.Caption;
            result.Code = workItem.Code;

            return result;
        }

        [WebMethod]
        public Models.WorkItem GetWorkItem(Guid projectId, Guid workItemCategoryId, Guid workItemId)
        {
            Models.WorkItem result = new Models.WorkItem();

            PM.Service.Model.WorkItem workItemCatgory = new CPPEI.PM.Service.Model.PMService().GetWorkItem(projectId, workItemCategoryId, workItemId);

            result.Id = workItemCatgory.Id;
            result.Caption = workItemCatgory.Caption;


            return result;
        }

        [WebMethod]
        public List<Models.WorkItem> GetWorkItemList(Guid projectId, Guid workItemCategoryId)
        {
            List<Models.WorkItem> result = new List<Models.WorkItem>();

            List<PM.Service.Model.WorkItem> workItemList = new CPPEI.PM.Service.Model.PMService().GetWorkItemList(workItemCategoryId);

            for (int index = 0; index < workItemList.Count; index++)
            {
                Models.WorkItem item = new Models.WorkItem();
                item.Id = workItemList[index].Id;
                item.Caption = workItemList[index].Caption;
                result.Add(item);
            }

            return result;
        }

        /// <summary>
        /// 更新单元格的信息
        /// </summary>
        /// <param name="account"></param>
        /// <param name="projectId"></param>
        /// <param name="workSheetId"></param>
        /// <param name="cells"></param>
        [WebMethod]
        public void UpdateWorkSheetCells(string account, Guid projectId, Guid workSheetId, Models.WorkSheetCell[] cells)
        {
            for (int index = 0; index < cells.Length; index++)
            {
                CPPEI.PM.Service.Model.WorkSheetCell newCell = new Service.Model.WorkSheetCell();
                newCell.Path = cells[index].Path;
                newCell.Value = cells[index].Value;
                newCell.Text = cells[index].Text;

                new PM.Service.Model.PMService().SaveWorkSheetCell(projectId, workSheetId, newCell);
            }
        }

        [WebMethod]
        public List<Models.WorkSheetCell> GetWorkSheetCells(string account, Guid projectId, Guid workSheetId)
        {
            List<Models.WorkSheetCell> result = new List<Models.WorkSheetCell>();
            List<CPPEI.PM.Service.Model.WorkSheetCell> list = new PM.Service.Model.PMService().GetWorkSheetCells(projectId, workSheetId);

            for (int index = 0; index < list.Count; index++)
            {
                Models.WorkSheetCell cell = new Models.WorkSheetCell();
                cell.Path = list[index].Path;
                cell.Value = list[index].Value;
                cell.Text = list[index].Text;
                result.Add(cell);
            }

            return result;
        }


    }
}
